package com.ruoyi.park.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.park.domain.MaMaterial;
import com.ruoyi.park.domain.vo.MaMaterialExcelVO;
import com.ruoyi.park.domain.vo.MaStockExportVO;
import com.ruoyi.park.service.IMaMaterialService;
import com.ruoyi.park.utils.POJOConverter;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 物资Controller
 *
 * @author cjie
 * @date 2021-09-16
 */
@Api(tags = " 物料清单")
@RestController
@RequestMapping("/maMaterial")
public class MaMaterialController extends BaseController {
    @Autowired
    private IMaMaterialService maMaterialService;

    /**
     * 查询所有物资
     */
    @ApiOperation(value="查询所有物资", notes="查询所有物资")
    @PreAuthorize(hasPermi = "park:maMaterial:list")
    @GetMapping("/list")
    public TableDataInfo list(MaMaterial maMaterial) {
        startPage();
        List<MaMaterial> list = maMaterialService.selectAllMaMaterialList(maMaterial);
        return getDataTable(list);
    }

    /**
     * 查询物资库存（分库）
     */
    @ApiOperation(value="查询物资库存（分库）", notes="查询物资库存（分库）")
    @PreAuthorize(hasPermi = "park:maMaterial:list")
    @GetMapping("/listStock")
    public TableDataInfo listStock(MaMaterial maMaterial) {
        startPage();
        List<MaMaterial> list = maMaterialService.selectHouseMaMaterialList(maMaterial);
        return getDataTable(list);
    }

    /**
     * 查询物资分类
     */
    @ApiOperation(value="小程序查询物资分类", notes="小程序查询物资分类")
    @PreAuthorize(hasPermi = "park:maMaterial:list")
    @GetMapping("/listCategory")
    public AjaxResult getCategory() {
        List<MaMaterial> list = maMaterialService.getCategory();
        return AjaxResult.success(list);
    }

    /**
     * 查询对应仓库物资列表
     */
    @ApiOperation(value="查询对应仓库物资列表", notes="查询对应仓库物资列表")
    @PreAuthorize(hasPermi = "park:maMaterial:list")
    @GetMapping("/listByWarehouse")
    public TableDataInfo listByWarehouse(MaMaterial maMaterial) {
        startPage();
        List<MaMaterial> list = maMaterialService.selectMaMaterialListByWarehouse(maMaterial);
        return getDataTable(list);
    }

    /**
     * 导出物资列表
     */
    @ApiOperation(value="导出物资列表", notes="导出物资列表")
    @PreAuthorize(hasPermi = "park:maMaterial:export")
    @Log(title = "物资", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(HttpServletResponse response, MaMaterial maMaterial) throws IOException {
        List<MaMaterial> list = maMaterialService.selectMaMaterialList(maMaterial);
        ExcelUtil<MaMaterial> util = new ExcelUtil<MaMaterial>(MaMaterial.class);
        util.exportExcel(response, list, "物资数据");
    }

    /**
     * 导出物资库存
     */
    @ApiOperation(value="导出物资库存", notes="导出物资库存")
    @PreAuthorize(hasPermi = "park:maMaterial:export")
    @Log(title = "物资库存", businessType = BusinessType.EXPORT)
    @GetMapping("/exportStock")
    public void exportStock(HttpServletResponse response, MaMaterial maMaterial) throws IOException {
        List<MaMaterial> list = maMaterialService.selectHouseMaMaterialList(maMaterial);
        List<MaStockExportVO> result = POJOConverter.copyList(list, MaStockExportVO.class);
        ExcelUtil<MaStockExportVO> util = new ExcelUtil<MaStockExportVO>(MaStockExportVO.class);
        util.exportExcel(response, result, "物资库存");
    }

    /**
     * 获取物资详细信息
     */
    @ApiOperation(value="获取物资详细信息", notes="获取物资详细信息")
    @PreAuthorize(hasPermi = "park:maMaterial:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(maMaterialService.selectMaMaterialById(id));
    }

    /**
     * 新增物资
     */
    @ApiOperation(value="新增物资", notes="新增物资")
    @PreAuthorize(hasPermi = "park:maMaterial:add")
    @Log(title = "物资", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MaMaterial maMaterial) {
        return maMaterialService.insertMaMaterial(maMaterial);
    }

    /**
     * 修改物资
     */
    @ApiOperation(value="修改物资", notes="修改物资")
    @PreAuthorize(hasPermi = "park:maMaterial:edit")
    @Log(title = "物资", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MaMaterial maMaterial) {
        return maMaterialService.updateMaMaterial(maMaterial);
    }

    /**
     * 删除物料
     */
    @ApiOperation(value="删除物资", notes="删除物资")
    @ApiImplicitParam(name = "ids", value = "物资ID数组", required = true,paramType = "path", allowMultiple=true, dataType = "String")
    @PreAuthorize(hasPermi = "park:maMaterial:remove")
    @Log(title = "物资", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(maMaterialService.deleteMaMaterialByIds(ids));
    }

    /**
     * 删除物资库存
     */
    @ApiOperation(value="删除物资库存", notes="删除物资库存")
    @ApiImplicitParam(name = "ids", value = "物资库存ID数组", required = true,paramType = "path", allowMultiple=true, dataType = "String")
    @PreAuthorize(hasPermi = "park:maMaterial:remove")
    @Log(title = "物资", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete/{ids}")
    public AjaxResult removeHouseMaMaterial(@PathVariable String[] ids) {
        return toAjax(maMaterialService.deleteHouseMaMaterialByIds(ids));
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        ExcelUtil<MaMaterialExcelVO> util = new ExcelUtil<MaMaterialExcelVO>(MaMaterialExcelVO.class);
        util.importTemplateExcel(response, "物料数据");
    }

    /**
     * 导入抄表记录列表,
     */
    @PreAuthorize(hasPermi = "park:maMaterial:edit")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<MaMaterialExcelVO> util = new ExcelUtil<MaMaterialExcelVO>(MaMaterialExcelVO.class);
        List<MaMaterialExcelVO> buiRoomVOList = util.importExcel(file.getInputStream());
        //批量插入数据
        String message = maMaterialService.importData(buiRoomVOList);
        return AjaxResult.success(message);

    }
}
